package cn.edu.neusoft.practice.Controller;

import cn.edu.neusoft.practice.mapper.ArticleMapper;
import cn.edu.neusoft.practice.po.Article;
import cn.edu.neusoft.practice.po.ArticleSearchForm;
import cn.edu.neusoft.practice.Service.ArticleService;
import cn.edu.neusoft.practice.vo.ArticlePageRes;
import cn.edu.neusoft.practice.mapper.ArticleMapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@CrossOrigin(origins = "*")
@RestController
public class ArticleController {

    @Autowired
    private ArticleMapper articleMapper;

    // 分页获取全部文章列表
    @RequestMapping("/getAllArticles")
    public ArticlePageRes getAllArticles(@RequestBody(required = false) ArticleSearchForm form) {
        // 参数兼容
        if (form == null) {
            form = new ArticleSearchForm();
            form.setPage(1L);
            form.setSize(100L);
        } else {
            if (form.getPage() == null || form.getPage() == 0) {
                form.setPage(1L);
            }
            if (form.getSize() == null || form.getSize() == 0) {
                form.setSize(100L);
            }
        }
        // 按照创建时间降序
        QueryWrapper<Article> wrapper = new QueryWrapper<Article>();
        wrapper.orderByDesc("create_time");
        Page<Article> articlePage = articleMapper.selectPage(new Page<Article>(form.getPage(), form.getSize()), wrapper);
        ArticlePageRes res = new ArticlePageRes();
        res.setData(articlePage.getRecords());
        res.setPage(articlePage.getCurrent());
        res.setSize(form.getSize());
        res.setPages(articlePage.getPages());
        res.setTotal(articlePage.getTotal());
        return res;
    }

    // 根据文章id获取文章
    @RequestMapping("/getArticle/{id}")
    public Article getArticle(@PathVariable("id") String articleId) {
        return articleMapper.selectById(articleId);
    }

    // 添加文章接口
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    public int addArticle(@RequestBody Article article) {
        article.setCreateTime(new Date());
        return articleMapper.insert(article);
    }

    // 根据文章id删除文章
    @RequestMapping(value = "/deleteArticle/{id}", method = RequestMethod.DELETE)
    public int deleteArticle(@PathVariable("id") String articleId) {
        return articleMapper.deleteById(articleId);
    }
    
//    //2逻辑删除的方法
//    @DeleteMapping("/deleteArticle/{id}")      //id值需要路径来传递
//    public String deleteArticlemanagement(String id){
//        if(ArticleService.removeById(id)){
//            return "1";
//        }
//        return "0";
//    }
//  


    // 跟进文章id更新文章接口
    @RequestMapping(value = "/updateArticle", method = RequestMethod.PUT)
    public int updateArticle(@RequestBody Article article) {
        if (article.getArticleId().equals("")) return 0;
        return articleMapper.updateById(article);
    }

    // 分页搜索接口，模糊匹配
    @RequestMapping(value = "/getCurrentArticle", method = RequestMethod.POST)
    public ArticlePageRes getCurrentArticle(@RequestBody ArticleSearchForm form) {
        // 参数兼容
        if (form == null) {
            form = new ArticleSearchForm();
            form.setPage(1L);
            form.setSize(100L);
        } else {
            if (form.getPage() == null || form.getPage() == 0) {
                form.setPage(1L);
            }
            if (form.getSize() == null || form.getSize() == 0) {
                form.setSize(100L);
            }
        }
        // 按照创建时间降序
        QueryWrapper<Article> wrapper = new QueryWrapper<Article>();
        wrapper.like("title", form.getKw());
        wrapper.orderByDesc("create_time");
        Page<Article> articlePage = articleMapper.selectPage(new Page<Article>(form.getPage(), form.getSize()), wrapper);
        ArticlePageRes res = new ArticlePageRes();
        res.setData(articlePage.getRecords());
        res.setPage(articlePage.getCurrent());
        res.setSize(form.getSize());
        res.setPages(articlePage.getPages());
        res.setTotal(articlePage.getTotal());
        return res;
    }
}
